﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;

namespace Wicresoft.RDW.Manage
{
    public partial class RoleView : BasePage_BackEnd
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                RoleViewDataSource();
            }
        }


        protected void lbtnToRight_OnClick(object sender, EventArgs e)
        {
            ListItem selectedItem = lstLeftAccount.SelectedItem;
            if (selectedItem != null)
                lstLeftAccount.Items.Remove(selectedItem);
        }

        protected void lbtnToLeft_OnClick(object sender, EventArgs e)
        {
            ListItem selectedItem = lstAllAccount.SelectedItem;

            if (selectedItem == null) return;
            foreach (ListItem li in lstLeftAccount.Items)
            {
                if (li.Value == selectedItem.Value)
                    return;
            }

            selectedItem.Selected = false;
            lstLeftAccount.Items.Add(selectedItem);
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            lblNameWarn.Text = string.Empty;
            if (string.IsNullOrWhiteSpace(txtName.Text))
            {
                lblNameWarn.Text = Resources.CommonResource.NotNull;
                return;
            }

            int roleId = 0;
            switch (PageStatus)
            {
                case Utility.PageStatus.Edit:
                    var role = RolePresent.GetById(Id);
                    if ((txtName.Text.Trim() != role.Name) && (RolePresent.GetByCondition(g => g.Name.Equals(txtName.Text.Trim())) != null))
                    {
                        lblNameWarn.Text = Resources.NameResource.Lable_Warn_wrong;
                        return;
                    }
                    role.Name = txtName.Text.Trim();
                    role.Desc = Server.HtmlEncode(txtDesc.Text);
                    RolePresent.Update(role);

                    var userRoles = UserRolePresent.GetList(g => g.RoleId == Id);
                    UserRolePresent.Delete(userRoles);

                    roleId = Id;
                    break;
                case Utility.PageStatus.New:

                    if ((RolePresent.GetByCondition(g => g.Name.Equals(txtName.Text.Trim())) != null))
                    {
                        lblNameWarn.Text = Resources.NameResource.Lable_Warn_wrong;
                        return;
                    }
                    Model.Role newRole = new Model.Role()
                    {
                        Name = txtName.Text.Trim(),
                        Desc = Server.HtmlEncode(txtDesc.Text)
                    };
                    RolePresent.Create(newRole);

                    roleId = RolePresent.GetByCondition(g => g.Name == newRole.Name).RoleId;
                    break;
            }

            foreach (ListItem li in lstLeftAccount.Items)
            {
                Model.UserRole userRole = new Model.UserRole()
                {
                    RoleId = roleId,
                    UserId = int.Parse(li.Value)
                };
                UserRolePresent.Create(userRole);
            }

            Response.Redirect("/Manage/RoleList.aspx");

        }

        protected void btnCancle_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("/Manage/RoleList.aspx");
        }

        protected void RoleViewDataSource()
        {
            if (string.Equals(GetQueryString("pageStatus"), "edit", StringComparison.CurrentCultureIgnoreCase))
            {
                var us = RolePresent.GetById(Id);
                txtName.Text = us.Name;
                txtDesc.Text = Server.HtmlDecode(us.Desc);
                var userRole = UserRolePresent.GetList(g => g.RoleId == Id);

                foreach (Model.UserRole ur in userRole)
                {
                    if (ur.UserId != -1)
                    {
                        var user = UserPresent.GetById(ur.UserId);
                        ListItem item = new ListItem()
                        {
                            Text = user.Account,
                            Value = user.UserId.ToString()
                        };
                        lstLeftAccount.Items.Add(item);
                    }
                    else
                    {
                        ListItem item = new ListItem()
                        {
                            Text = "AllUser",
                            Value = "-1"
                        };
                        lstLeftAccount.Items.Insert(0, item);
                    }
                }
                PageStatus = Utility.PageStatus.Edit;
            }
            else if (string.Equals(GetQueryString("pageStatus"), "new", StringComparison.CurrentCultureIgnoreCase))
            {
                PageStatus = Utility.PageStatus.New;
            }
            else
            {
                Response.Redirect("/Manage/RoleList.aspx");
            }
        }

        public override void InitControlResource()
        {
            lblName.Text = Resources.NameResource.Role_Name + ":";
            lblDesc.Text = Resources.NameResource.Role_Desc;
            lblUser.Text = Resources.NameResource.Role_Account;

            lbtnSave.Text = Resources.NameResource.btnSubmit;
            lbtnCancle.Text = Resources.NameResource.btnCancel;

            lstAllAccount.DataSource = UserPresent.GetList().OrderBy(g => g.Account);
            lstAllAccount.DataTextField = "Account";
            lstAllAccount.DataValueField = "UserId";
            lstAllAccount.DataBind();
            ListItem item = new ListItem()
            {
                Text = "AllUser",
                Value = "-1"
            };
            lstAllAccount.Items.Insert(0, item);
        }
    }
}